package com.library.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.library.entity.StudentInfo;
import com.library.mapper.StudentInfoMapper;
import com.library.vo.P;
import com.library.vo.StudentInfoReq;
import com.library.vo.StudentInfoRes;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentInfoService {

    @Autowired
    private StudentInfoMapper studentInfoMapper;

    public P<List<StudentInfoRes>> list(StudentInfoReq studentInfo) {
        Page<Object> objects = PageHelper.startPage(studentInfo.getPageNum(), studentInfo.getPageSize());
        StudentInfo userInfo = new StudentInfo();
        BeanUtils.copyProperties(studentInfo,userInfo);
        List<StudentInfo> studentInfos = this.studentInfoMapper.selectByQuery(userInfo);
        P p = P.valueOf((Page) studentInfos);
        List<StudentInfoRes> res = new ArrayList<>();
        for (StudentInfo info : studentInfos) {
            StudentInfoRes e = new StudentInfoRes();
            BeanUtils.copyProperties(info, e);
            res.add(e);
        }
        p.setData(res);
        return p;
    }

    public void add(StudentInfo studentInfo) {
        int i = studentInfoMapper.insertSelective(studentInfo);
    }

    public void delete(StudentInfo studentInfo) {
        this.studentInfoMapper.deleteByPrimaryKey(studentInfo.getId());
    }

    public void update(StudentInfo studentInfo) {
        this.studentInfoMapper.updateByPrimaryKeySelective(studentInfo);
    }

    public StudentInfoRes detail(StudentInfo studentInfo) {
        StudentInfo studentInfo1 = this.studentInfoMapper.selectByPrimaryKey(studentInfo.getId());
        StudentInfoRes e = new StudentInfoRes();
        BeanUtils.copyProperties(studentInfo1, e);
        return e;
    }
}
